package duck;

/**
 * 鸭子父类
 * 包含颜色、体重属性和飞行、叫的方法
 */
public class Duck {
    protected String color;
    protected double heavy;

    public Duck() {
        this.color = "未知";
        this.heavy = 0.0;
    }

    public Duck(String color, double heavy) {
        this.color = color;
        this.heavy = heavy;
    }

    public void fly() {
        System.out.println("鸭子正在飞行");
    }

    public void quack() {
        System.out.println("鸭子正在叫");
    }

    // 重载setHeavy方法 - 在父类中展示重载
    public void setHeavy(double heavy) {
        this.heavy = heavy;
        System.out.println("设置鸭子体重为: " + heavy + "kg");
    }

    // 重载版本1 - 接受整数
    public void setHeavy(int heavy) {
        this.heavy = heavy;
        System.out.println("重载方法: 设置鸭子体重为整数: " + heavy + "kg");
    }

    // 重载版本2 - 接受字符串
    public void setHeavy(String weightStr) {
        try {
            this.heavy = Double.parseDouble(weightStr);
            System.out.println("重载方法: 设置鸭子体重为字符串: " + weightStr + "kg");
        } catch (NumberFormatException e) {
            System.out.println("体重格式错误");
        }
    }

    public void display() {
        System.out.println("这是一只" + color + "的鸭子，体重: " + heavy + "kg");
    }
}
